﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StrategyViewControl.cs" company="">
//   
// </copyright>
// <summary>
//   The strategy view control.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Drawing;
using System.Windows.Forms;
using Systemathics.FrameWork;
using Systemathics.UserControls.Properties;

namespace Systemathics.UserControls
{
    [ToolboxBitmap(typeof (StrategyViewControl), "UserControlsIcon.bmp")]
    public sealed partial class StrategyViewControl : UserControl
    {
        private readonly DataGridViewCellStyle DownStyle = new DataGridViewCellStyle {ForeColor = Color.Red};
        private delegate void InvokeDelegate();
        private readonly InvokeDelegate pUpdateStrategyView;
        private readonly DataGridViewCellStyle UpStyle = new DataGridViewCellStyle {ForeColor = Color.Green};
        private StrategyExposition InventoryExposition;
        private int SelectedRowIndex;
        private Strategy Strategy;
        private StrategyExposition TheoExposition;
        private StrategyExposition TradedExposition;
        private StrategyExposition WorkingExposition;

        public StrategyViewControl()
        {
            InitializeComponent();
            Dock = DockStyle.Fill;
            DoubleBuffered = true;

            pUpdateStrategyView = new InvokeDelegate(UpdateStrategyView);

            StrategyViewGrid.Rows.Add();
            StrategyViewGrid.Rows.Add();
            StrategyViewGrid.Rows.Add();
            StrategyViewGrid.Rows.Add();

            var stylee = new DataGridViewCellStyle{Font = new Font("Arial", 14.0f, FontStyle.Bold, GraphicsUnit.Pixel), BackColor = Color.LightGray};
            StrategyViewGrid.Rows[0].HeaderCell.Style = stylee;
            StrategyViewGrid.Rows[0].DefaultCellStyle = stylee;
            StrategyViewGrid.Rows[0].HeaderCell.Value = "THEO";
            StrategyViewGrid.Rows[1].HeaderCell.Value = "WORKING";
            StrategyViewGrid.Rows[2].HeaderCell.Value = "TRADED";
            StrategyViewGrid.Rows[3].HeaderCell.Value = "INVENTORY";
        }

        public Strategy SelectedStrategy
        {
            set
            {
                Strategy = value;
                TheoExposition = Strategy[Expo.Theo];
                WorkingExposition = Strategy[Expo.Working];
                TradedExposition = Strategy[Expo.Traded];
                InventoryExposition = Strategy[Expo.Inventory];
                
                // UpdateStrategyView();
            }
        }

        public new void Update()
        {
            StrategyViewGrid.BeginInvoke(pUpdateStrategyView);
        }

        private void UpdateStrategyView()
        {
            StrategyViewGrid["HasPosition", 0].Value = (TheoExposition.NetExposedNominal == 0)? Resources.En_ligne: Resources.Hors_ligne;
            StrategyViewGrid["LongExposedNominal", 0].Value = TheoExposition.LongExposedNominal;
            StrategyViewGrid["ShortExposedNominal", 0].Value = TheoExposition.ShortExposedNominal;
            StrategyViewGrid["NetExposedNominal", 0].Value = TheoExposition.NetExposedNominal;
            StrategyViewGrid["Realized", 0].Value = TheoExposition.Realized;
            StrategyViewGrid["UnRealized", 0].Value = TheoExposition.UnRealized;
            StrategyViewGrid["HasPosition", 1].Value = (Strategy[Expo.Working].NetExposedNominal != 0)? Resources.Hors_ligne: Resources.En_ligne;
            StrategyViewGrid["LongExposedNominal", 1].Value = WorkingExposition.LongExposedNominal;
            StrategyViewGrid["ShortExposedNominal", 1].Value = WorkingExposition.ShortExposedNominal;
            StrategyViewGrid["NetExposedNominal", 1].Value = WorkingExposition.NetExposedNominal;
            StrategyViewGrid["Realized", 1].Value = WorkingExposition.Realized;
            StrategyViewGrid["UnRealized", 1].Value = WorkingExposition.UnRealized;
            StrategyViewGrid["Bias", 1].Value = WorkingExposition.Bias*100;
            StrategyViewGrid["HasPosition", 2].Value = (Strategy[Expo.Traded].NetExposedNominal != 0)? Resources.Hors_ligne: Resources.En_ligne;
            StrategyViewGrid["LongExposedNominal", 2].Value = TradedExposition.LongExposedNominal;
            StrategyViewGrid["ShortExposedNominal", 2].Value = TradedExposition.ShortExposedNominal;
            StrategyViewGrid["NetExposedNominal", 2].Value = TradedExposition.NetExposedNominal;
            StrategyViewGrid["Realized", 2].Value = TradedExposition.Realized;
            StrategyViewGrid["UnRealized", 2].Value = TradedExposition.UnRealized;
            StrategyViewGrid["Bias", 2].Value = TradedExposition.Bias*100;
            StrategyViewGrid["HasPosition", 3].Value = (Strategy[Expo.Inventory].NetExposedNominal != 0)? Resources.Hors_ligne: Resources.En_ligne;
            StrategyViewGrid["LongExposedNominal", 3].Value = InventoryExposition.LongExposedNominal;
            StrategyViewGrid["ShortExposedNominal", 3].Value = InventoryExposition.ShortExposedNominal;
            StrategyViewGrid["NetExposedNominal", 3].Value = InventoryExposition.NetExposedNominal;
            StrategyViewGrid["Realized", 3].Value = InventoryExposition.Realized;
            StrategyViewGrid["UnRealized", 3].Value = InventoryExposition.UnRealized;
            StrategyViewGrid["Bias", 3].Value = InventoryExposition.Bias*100;
        }

        private void StrategyViewGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (StrategyViewGrid[e.ColumnIndex, e.RowIndex].Value != null)
            {
                string s = StrategyViewGrid[e.ColumnIndex, e.RowIndex].OwningColumn.Name;
                if (s == "PL")
                {
                    if ((double) StrategyViewGrid[e.ColumnIndex, e.RowIndex].Value >= 0)
                        StrategyViewGrid[e.ColumnIndex, e.RowIndex].Style = UpStyle;
                    else
                        StrategyViewGrid[e.ColumnIndex, e.RowIndex].Style = DownStyle;
                }
            }
        }
        private void StrategyViewGrid_MouseDown(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo Hti;
            if (e.Button == MouseButtons.Right)
            {
                Hti = StrategyViewGrid.HitTest(e.X, e.Y);
                SelectedRowIndex = Hti.RowIndex;
                if (Hti.Type == DataGridViewHitTestType.Cell)
                {
                    var Line = (String) StrategyViewGrid.Rows[SelectedRowIndex].HeaderCell.Value;
                    if (Line == "INVENTORY" || Line == "TRADED")
                    {
                        if (Strategy.isRunning)
                        {
                            StrategyViewGrid.ContextMenuStrip = ContextMenuStripFlatten;
                            ContextMenuStripFlatten.Show(StrategyViewGrid, new Point(e.X, e.Y));
                        }
                    }
                    else if (Line == "WORKING")
                    {
                        if (Strategy.isRunning)
                        {
                            StrategyViewGrid.ContextMenuStrip = ContextMenuStripCancel;
                            ContextMenuStripCancel.Show(StrategyViewGrid, new Point(e.X, e.Y));
                        }
                    }

                    StrategyViewGrid.Rows[SelectedRowIndex].Selected = true;
                }
            }
        }
        private void flattenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (StrategyViewGrid.Rows[SelectedRowIndex].HeaderCell.Value.ToString() == "TRADED")
            {
                foreach (Instrument i in Strategy.Instruments.Values)
                    i.SweepToFill(Expo.Traded, Strategy.strategy.Id);
            }
            else if (StrategyViewGrid.Rows[SelectedRowIndex].HeaderCell.Value.ToString() == "INVENTORY")
            {
                foreach (Instrument i in Strategy.Instruments.Values)
                    i.SweepToFill(Expo.Inventory, Strategy.strategy.Id);
            }
        }
        private void CANCELALLtoolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (StrategyViewGrid.Rows[SelectedRowIndex].HeaderCell.Value.ToString() == "WORKING")
                foreach (Instrument i in Strategy.Instruments.Values)
                {
                    int number = i.CancelAllOrders();
                    new RemotingLogClient().Trace(new FireMessageEventArgs(number + " Orders Canceled for " + i.StaticData.Name, DateTime.Now.TimeOfDay));
                }
        }
        private void CANCELLALLBUYStoolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (StrategyViewGrid.Rows[SelectedRowIndex].HeaderCell.Value.ToString() == "WORKING")
                foreach (Instrument i in Strategy.Instruments.Values)
                {
                    int number = i.CancelAllBuyOrders();
                    new RemotingLogClient().Trace(new FireMessageEventArgs(number + " Buy Orders Canceled for " + i.StaticData.Name, DateTime.Now.TimeOfDay));
                }
        }
        private void CANCELLALLSELLStoolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (StrategyViewGrid.Rows[SelectedRowIndex].HeaderCell.Value.ToString() == "WORKING")
                foreach (Instrument i in Strategy.Instruments.Values)
                {
                    int number = i.CancelAllSellOrders();
                    new RemotingLogClient().Trace(new FireMessageEventArgs(number + " Sell Orders Canceled for " + i.StaticData.Name, DateTime.Now.TimeOfDay));
                }
        }
    }
}